﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ModemGSM;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class UCSMS : UserControl
    {
        public delegate void UCSMSSelectedEventHandler(object sender, EventArgs e);
        public event UCSMSSelectedEventHandler EventUCSMSSeleted;

        public delegate void UCSMSCheckedEventHandler(object sender, EventArgs e);
        public event UCSMSCheckedEventHandler EventUCSMSChecked;

        
        private bool NotLoad;
        
        public UCSMS()
        {
            InitializeComponent();
            TypeMsg = LocationSMS.All;
            LblDtHr.Text = "";
            LblNumber.Text = "";
            LblName.Text = "";
            LblTz.Text = "";
            LblMsg.Text = "";
            NotLoad = true;
        }

        public int IndexSMS { get; set; }
        public bool Checked 
        {
            get { return ChkDel.Checked; } 
            set { ChkDel.Checked = value; }
        }
        public LocationSMS TypeMsg { get; set; }

        private DateTime? _DateTime = null;
        public DateTime? DateTime 
        {
            get { return _DateTime; }
            set 
            {
                _DateTime = value;
            }
        }
        private string _Number = "";
        public string Number 
        {
            get { return _Number; }
            set { _Number = value; }
        }
        private string _NamePhonebook = "";
        public string NamePhonebook 
        {
            get { return _NamePhonebook; }
            set { _NamePhonebook = value; }
        }
        public string Message 
        {
            get { return LblMsg.Text; }
            set { LblMsg.Text = value; }
        }

        private TimeSpan? _Timezone = null;
        public TimeSpan? Timezone 
        {
            get { return _Timezone; }
            set 
            {
                _Timezone = value;
            }
        }

        private bool _Seleted = false;
        public bool Seleted 
        { 
            get { return _Seleted; } 
            set
            {
                _Seleted = value;
                if (value)
                {
                    this.BackColor = Color.Honeydew;
                    this.BorderStyle = BorderStyle.FixedSingle;
                }
                else
                {
                    this.BackColor = Color.Transparent;
                    this.BorderStyle = BorderStyle.None;
                }
                if (EventUCSMSSeleted != null && !NotLoad)
                {
                    EventUCSMSSeleted(this, new EventArgs());
                }
            } 
        }

        private void UCSMS_Leave(object sender, EventArgs e)
        {
            this.Seleted = false;
        }

        private void UCSMS_Click(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }

        private void LblNumber_Click(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }

        private void LblDtHr_Click(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }

        private void LblMsg_Click(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }

        private void UCSMS_Enter(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }

        private void UCSMS_Load(object sender, EventArgs e)
        {
            NotLoad = false;
            if (!_DateTime.HasValue)
            {
                LblDtHr.Text = "";
                LblDtHr.Visible = false;
            }
            else
            {
                DateTime Dt = _DateTime.Value;
                if (_Timezone.HasValue)
                {
                    TimeSpan ts = System.TimeZone.CurrentTimeZone.GetUtcOffset(System.DateTime.Now);
                    ts = ts.Subtract(_Timezone.Value);
                    Dt.Add(ts);
                }
                string Fmt = Properties.Settings.Default.DefaultDateFmt;
                Fmt = Fmt.Replace("DD", "dd").Replace("YYYY", "yyyy");
                LblDtHr.Text = ResourcesText.WordDate + ": " + Dt.ToString(Fmt + " HH:mm:ss");
            }
            if (!_Timezone.HasValue || _Timezone.Value.TotalSeconds == 0)
            {
                LblTz.Text = "";
                LblTz.Visible = false;
            }
            else
            {
                TimeSpan ts = System.TimeZone.CurrentTimeZone.GetUtcOffset(System.DateTime.Now);
                ts = ts.Subtract(_Timezone.Value);
                if (ts.TotalMinutes < 0)
                {
                    LblTz.Text = "-" + ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.ToString().PadLeft(2, '0');
                }
                else
                {
                    if (ts.TotalSeconds != 0)
                    {
                        LblTz.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.Minutes.ToString().PadLeft(2, '0');
                    }
                    else
                    {
                        LblTz.Text = "";
                        LblTz.Visible = false;
                    }
                }
            }
            if (_Number == "")
            {
                LblNumber.Text = "";
                LblNumber.Visible = false;
            }
            else
            {
                LblNumber.Text = ResourcesText.WordNumber +  ": " + _Number;
            }
            if (_NamePhonebook == "")
            {
                LblName.Text = "";
                LblName.Visible = false;
            }
            else
            {
                LblName.Text = ResourcesText.WordName + ": " + _NamePhonebook;
            }
        }

        private void LblName_Click(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }

        private void ChkDel_CheckedChanged(object sender, EventArgs e)
        {
            if (EventUCSMSChecked != null)
            {
                EventUCSMSChecked(this, new EventArgs());
            }
        }

        private void flowLayoutPanelDet_Click(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }

        private void LblTz_Click(object sender, EventArgs e)
        {
            this.Seleted = true;
            this.Focus();
        }
    }

}
